/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sadhar.sipp.anggaranpendapatansewabungadll;

import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.data.JRTableModelDataSource;
import org.sadhar.sia.common.ClassConnection;
import org.sadhar.sia.framework.ClassApplicationModule;
import org.sadhar.sipp.laporananggaranpendapatanuangformulirmahasiswabaru.ResultSetTableModel;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zkex.zul.Jasperreport;
import org.zkoss.zul.Button;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Panel;
import org.zkoss.zul.Radio;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

/**
 *
 * @author hendro
 */
public class AnggaranPendapatanSewaBungaDllWnd extends ClassApplicationModule {

    private Checkbox chkboxPosPendapatan;
    private Textbox txtboxPosPendapatan;
    private Checkbox chkboxUnitKerja;
    private Textbox txtboxUnitKerja;
    private Combobox cmbTahunAnggaranMulai;
    private Combobox cmbTahunAnggaranSelesai;
    private Radio rdTahun;
    private Radio rdPosPendapatan;
    private Radio rdUnitKerja;
    private Textbox txtboxIdPosPendapatan;
    private Textbox txtboxKodeUnitKerja;
    private Listbox lstboxFormatLaporan;
    private Button buttonExport;
    private Button buttonHideExport;
    private Panel panelReport;
    private Jasperreport report;
    private AnggaranPendapatanSewaBungaDllDAO dao;

    public AnggaranPendapatanSewaBungaDllWnd() {
        dao = new AnggaranPendapatanSewaBungaDllDAOImpl();
    }

    public void onCreate() throws Exception {
        chkboxPosPendapatan = (Checkbox) getFellow("chkboxPosPendapatan");
        txtboxPosPendapatan = (Textbox) getFellow("txtboxPosPendapatan");
        chkboxUnitKerja = (Checkbox) getFellow("chkboxUnitKerja");
        txtboxUnitKerja = (Textbox) getFellow("txtboxUnitKerja");
        cmbTahunAnggaranMulai = (Combobox) getFellow("cmbTahunAnggaranMulai");
        cmbTahunAnggaranSelesai = (Combobox) getFellow("cmbTahunAnggaranSelesai");
        rdTahun = (Radio) getFellow("rdTahun");
        rdPosPendapatan = (Radio) getFellow("rdPosPendapatan");
        rdUnitKerja = (Radio) getFellow("rdUnitKerja");
        txtboxIdPosPendapatan = (Textbox) getFellow("txtboxIdPosPendapatan");
        txtboxKodeUnitKerja = (Textbox) getFellow("txtboxKodeUnitKerja");
        lstboxFormatLaporan = (Listbox) getFellow("lstboxFormatLaporan");
        buttonExport = (Button) getFellow("buttonExport");
        buttonHideExport = (Button) getFellow("buttonHideExport");
        panelReport = (Panel) getFellow("panelReport");
        report = (Jasperreport) getFellow("report");
        loadTahunAnggaran();
        rdTahun.setChecked(true);
    }

    public void btnCariPosPendapatanOnClick() throws Exception {
        final Window win = (Window) Executions.createComponents("/sippzul/pospendapatan/PosPendapatanPopup.zul", this, null);
        Textbox txtboxKeySearch = (Textbox) win.getFellow("txtboxKeySearch");
        txtboxKeySearch.setValue(txtboxPosPendapatan.getValue());
        win.doModal();
    }

    public void btnCariUnitKerjaOnClick() throws Exception {
        final Window win = (Window) Executions.createComponents("/sippzul/unitkerja/UnitKerjaPopup.zul", this, null);
        Textbox txtboxKeySearch = (Textbox) win.getFellow("txtboxKeySearch");
        txtboxKeySearch.setValue(txtboxUnitKerja.getValue());
        win.doModal();
    }

    private void loadTahunAnggaran() throws Exception {
        cmbTahunAnggaranMulai.getItems().clear();
        cmbTahunAnggaranSelesai.getItems().clear();
        for (String str : dao.getTahunAnggaran()) {
            cmbTahunAnggaranMulai.appendChild(new Comboitem(str));
            cmbTahunAnggaranSelesai.appendChild(new Comboitem(str));
        }
    }

    public void hideExport() {
        panelReport.setVisible(false);
        buttonHideExport.setVisible(false);
    }

    public void export() throws Exception {
        if (cmbTahunAnggaranMulai.getValue().isEmpty() || cmbTahunAnggaranSelesai.getValue().isEmpty()) {
            Messagebox.show("Silahkan Pilih Periode", "Pesan Informasi", Messagebox.OK, Messagebox.INFORMATION);
            return;
        } else {
            report.setType(lstboxFormatLaporan.getSelectedItem().getValue().toString());
            if (rdTahun.isChecked()) {
                report.setSrc("reports/laporananggaranpendapatansewabungadll/LaporanPenyusunanAnggaranPendapatanSewaBungaDll.jasper");
            } else if (rdPosPendapatan.isChecked()) {
                report.setSrc("reports/laporananggaranpendapatansewabungadll/LaporanPenyusunanAnggaranPendapatanSewaBungaDllGroupByPosPendapatan.jasper");
            } else if (rdUnitKerja.isChecked()) {
                report.setSrc("reports/laporananggaranpendapatansewabungadll/LaporanPenyusunanAnggaranPendapatanSewaBungaDllGroupByUnitKerja.jasper");
            }
            String THN1 = cmbTahunAnggaranMulai.getValue();
            String THN2 = cmbTahunAnggaranSelesai.getValue();
            String IDPOS = "";
            String KODEUNIT = "";
            Map m = new HashMap();
            m.put("THN1", THN1);
            m.put("THN2", THN2);
            if (chkboxPosPendapatan.isChecked()) {
                //m.put("ID_POS_PENDAPATAN", "%" + txtboxIdPosPendapatan + "%");
                IDPOS = "%" + txtboxIdPosPendapatan.getValue() + "%";
            } else {
                //m.put("ID_POS_PENDAPATAN", "%%");
                IDPOS = "%%";
            }
            if (chkboxUnitKerja.isChecked()) {
                //m.put("KODE_UNIT", "%" + txtboxKodeUnitKerja + "%");
                KODEUNIT = "%" + txtboxKodeUnitKerja.getValue() + "%";
            } else {
                //m.put("KODE_UNIT", "%%");
                KODEUNIT = "%%";
            }
            report.setParameters(m);

            ResultSetTableModel rstm = new ResultSetTableModel(ClassConnection.getJdbc().getDataSource().getConnection(), dao.getSQL(THN1, THN2, IDPOS, KODEUNIT));
            report.setDatasource(new JRTableModelDataSource(rstm));
            showExport();
        }
    }

    public void showExport() {
        panelReport.setVisible(true);
        panelReport.setOpen(true);
        buttonHideExport.setVisible(true);
    }
}
